package com.链表2;

import java.util.*;

/**
 * 将两个升序链表合并为一个新的升序链表并返回。新链表是通过拼接给定的两个链表的所有节点组成的。
 *
 * 示例：
 *
 * 输入：1->2->4, 1->3->4
 * 输出：1->1->2->3->4->4
 */
public class 合并两个有序链表 {
    static class Solution {
        public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
            List<Integer> list = new ArrayList<>();
            ListNode pr1 = l1;
            ListNode pr2 = l2;
           while (pr1 != null){
                   list.add(pr1.val);
                   pr1 = pr1.next;
           }
           while (pr2 != null){
                list.add(pr2.val);
                pr2 = pr2.next;
           }
            Collections.sort(list);
           ListNode pr3 = new ListNode(-1);
           ListNode pr4 = pr3;
           for(Integer number:list){
               pr4.next = new ListNode(number);
               pr4 = pr4.next;
           }
            return pr3.next;
        }
    }

    public static void main(String[] args) {
        MyLinkedList link = new MyLinkedList(new int[]{1,2,3});
        Solution solution  = new Solution();
        ListNode node = solution.mergeTwoLists(link.head.next,link.head.next);

    }
}
